<template>
  <div class="login-container">
    <div>面经登录</div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        label="用户名"
        placeholder="用户名"
        required
      />
      <van-field
        v-model="password"
        type="password"
        label="密码"
        placeholder="密码"
        required
      />
      <div style="margin: 16px">
        <van-button block type="primary" color="#FF6700" @click="login"
          >提交</van-button
        >
      </div>
    </van-form>
    <div class="register-link">
      <router-link to="/register">注册账号</router-link>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HmLogin",
  data() {
    return {
      username: "",
      password: "",
    };
  },

  methods: {
    async login(username = this.username, password = this.password) {
      if (username.length < 5 || password.length < 6) {
        return "账号或密码错误";
      }
      const res = await axios({
        url: "http://interview-api-t.itheima.net/h5/user/login",
        method: "POST",
        data: {
          username: this.username,
          password: this.password,
        },
      });
      if (res.data.code == 10000) {
        this.$router.push({ path: "/layout?token=" + res.data.data.token });
      }
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.register-link {
  margin-top: 20px;
  text-align: right;
}
</style>
